var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menustat: 0,
    menuitem: [
      { teamnum: '全部',status:0 },
      { teamnum: '待付款', status: 2 },
      { teamnum: '待发货', status: 3 },
      { teamnum: '待收货', status: 4 },
      { teamnum: '已完成', status: 99 }
    ],
    page_num:1,
  },
  selectmenu: function (e) {
    var page = this
    
    console.log(e);
    this.setData({
      menustat: e.currentTarget.dataset.key,
    });
    page.getMyorder(e.currentTarget.dataset.status)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({

    })
    this.getMyorder();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },

  getMyorder:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.my_order,
      method: 'POST',
      data: {
        status: page.data.menuitem[page.data.menustat].status == 0 ? '' : page.data.menuitem[page.data.menustat].status,
        page:page.data.page_num
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            order: res.data.data,
            page_data:res.data.page_data
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  doPay:function(e){
    var page = this
    wx.showLoading({
      title: '加载中',
    })
    //订单获取支付
    app.request({
      url: api.order.get_pay_data,
      method: 'POST',
      data: {
        order_id: e.currentTarget.dataset.core_id,
        pay_type: 'WECHAT_PAY'
      },
      success: function (result) {
        wx.hideLoading();
        if (result.errcode == 0) {
          var pay_data = result.data;
          wx.requestPayment({
            timeStamp: pay_data.timeStamp,
            nonceStr: pay_data.nonceStr,
            package: pay_data.package,
            signType: pay_data.signType,
            paySign: pay_data.paySign,
            success: function (pres) {
              wx.showToast({
                title: '订单支付成功',
                icon: 'success'
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: '../pay-success/pay-success?order_id=' + e.currentTarget.dataset.id,
                })
              }, 1000)
            },
            fail: function (res) {
              wx.showToast({
                title: '订单未支付',
                image: '/images/icon-warning.png'
              });
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: result.msg,
            showCancel: false
          });
        }
      }
    });
  },
  //删除订单
  doDelete:function(e){
    var page = this;
    wx.showModal({
      title: '提示',
      content: '确认删除',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '加载中',
          })
          app.request({
            url: api.shop.del_order,
            method: 'POST',
            data: {
              order_id: e.currentTarget.dataset.id,
            },
            success: function (res) {
              if (res.errcode == 0) {
                wx.showToast({
                  title: '删除成功',
                })
                page.onShow()
                
              }else{
                wx.showModal({
                  title: '警告',
                  content: res.errmsg,
                  showCancel: false
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '警告',
                content: res.errmsg,
                showCancel: false
              })
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        }
      }
    })
  },
  //取消订单
  doCancel: function (e) {
    var page = this;
    wx.showModal({
      title: '提示',
      content: '确认取消',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
          })
          app.request({
            url: api.shop.cancel_order,
            method: 'POST',
            data: {
              order_id: e.currentTarget.dataset.id,
            },
            success: function (res) {
              if (res.errcode == 0) {
                wx.showToast({
                  title: '取消成功',
                })
              } else {
                wx.showModal({
                  title: '警告',
                  content: res.errmsg,
                  showCancel: false
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '警告',
                content: res.errmsg,
                showCancel: false
              })
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        }
      }
    })
  },
  //确认收货
  doConfirm:function(e){
    var page = this;
    wx.showModal({
      title: '提示',
      content: '确认收货',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
          })
          app.request({
            url: api.shop.confirm_order,
            method: 'POST',
            data: {
              order_id: e.currentTarget.dataset.id,
            },
            success: function (res) {
              if (res.errcode == 0) {
                wx.showToast({
                  title: '操作成功',
                })
              } else {
                wx.showModal({
                  title: '警告',
                  content: res.errmsg,
                  showCancel: false
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '警告',
                content: res.errmsg,
                showCancel: false
              })
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        }
      }
    })
  },

  //去评价
  doComment:function(e){
    console.log(e);
    var goods_id = e.currentTarget.dataset.goods_id;
    var order_id = e.currentTarget.dataset.order_id;
    var ordergoods_id = e.currentTarget.dataset.ordergoods_id;
    wx.navigateTo({
      url: '../order-evaluation/order-evaluation?ordergoods_id=' + ordergoods_id+'&goods_id='+goods_id,
    })
  }

})